from rest_framework import serializers
from .models import *
import re

class UserSer(serializers.ModelSerializer):
    #自定义一个验证方法，来验证手机号字段
    def validate_phone(self,phone):
        if not re.match("^1[3-9]\d{9,9}$",phone):
            #判断不满足手机号条件
            raise serializers.ValidationError(detail="手机号格式错误",code=400)
        return phone
    class Meta:
        model = UserProfile
        fields = '__all__'

    def create(self,validated_data):
        return UserProfile.objects.create_user(**validated_data)#  username=123,age=20